package cn.fl.system.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.framework.api.ISysCtrlCfgApiService;
import cmm.comm.facade.framework.condition.SysCtrlCfgQueryCondition;
import cmm.comm.facade.framework.dto.SysCtrlCfgDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysCtrlCfgService;
import cn.fl.system.transform.SysCtrlCfgVOTransform;
import cn.fl.system.vo.SysCtrlCfgVO;

/**
 * @Description: 系统控制配置表 客户端服务层接口实现
 */
@Service
public class SysCtrlCfgServiceImpl implements ISysCtrlCfgService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysCtrlCfgServiceImpl.class);
	
	/**
	 * TODO 系统控制配置信息 API 暴露服务
	 */
	@Autowired
	private ISysCtrlCfgApiService sysCtrlCfgApiService;

	@Override
	public SysCtrlCfgVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "根据主键查询系统控制配置信息失败,主键参数为空.ID:"+id, CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		SysCtrlCfgDTO sysCtrlCfgDTO = this.sysCtrlCfgApiService.selectSysCtrlCfgById(id,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		return SysCtrlCfgVOTransform.toVO(sysCtrlCfgDTO);
	}

	@Override
	public Long add(SysCtrlCfgVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增系统控制配置失败,系统控制配置对象为空.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.sysCtrlCfgApiService.addSysCtrlCfg(SysCtrlCfgVOTransform.toDTO(entity));
	}

	@Override
	public boolean modify(SysCtrlCfgVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改系统控制配置失败,系统控制配置对象为空.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysCtrlCfgApiService.modifySysCtrlCfg(SysCtrlCfgVOTransform.toDTO(entity));
	}


	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException {
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除系统控制配置失败,主键参数为空.IDS:"+JSONObject.toJSONString(ids),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysCtrlCfgApiService.removeSysCtrlCfgByIds(ids);
	}

	@Override
	public Map<String,String> searchList(SysCtrlCfgQueryCondition qc)
			throws BaseException {
		if(null == qc || StringUtils.isBlank(qc.getSysCd()) || StringUtils.isBlank(qc.getOrgCd())){
			BaseLogUtils.info(logger, "searchList", "查询系统控制配置信息失败,需要必要的查询条件:系统代码和组织架构代码.DATA:"+JSONObject.toJSONString(qc),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.sysCtrlCfgApiService.searchSysCtrlCfgList(qc);
	}


	@Override
	public String searchSysCtrlCfgByCtrlCode(SysCtrlCfgQueryCondition qc)
			throws BaseException {
		if(null == qc || StringUtils.isBlank(qc.getSysCd()) || StringUtils.isBlank(qc.getOrgCd()) || StringUtils.isBlank(qc.getCtrlCode())){
			BaseLogUtils.info(logger, "searchSysCtrlCfgByCtrlCode", "根据条件查询系统控制配置值失败,需要必要的查询条件:系统代码,组织架构代码和配置代码.DATA:"+JSONObject.toJSONString(qc),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCode(qc);
	}
}
